
package lt.andro.seimorinkimai2012;

import lt.andro.utils.GoogleAnalyticsEventsHelper;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebBackForwardList;
import android.webkit.WebHistoryItem;
import android.webkit.WebView;
import android.widget.Toast;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.view.Window;

public class MainActivity extends SherlockActivity {

    protected static final String TAG = MainActivity.class.getCanonicalName();
    private static final String EXTRA_WEB_URL = "EXTRA_WEB_URL";
    public static final String HOME_URL = "file:///android_asset/index.html";
    private WebView mWebView;
    private GoogleAnalyticsEventsHelper mGoogleAnalyticsEventsHelper;
    private SrWebViewClient mWebViewClient;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        setContentView(R.layout.activity_main);
        setSupportProgressBarIndeterminateVisibility(false);
        mWebView = (WebView)findViewById(R.id.activity_main_wv);
        mGoogleAnalyticsEventsHelper = GoogleAnalyticsEventsHelper
                .getInstance(getApplicationContext());
        mWebViewClient = new SrWebViewClient(this, mGoogleAnalyticsEventsHelper, mWebView);
        mWebView.setWebViewClient(mWebViewClient);

        ActionBar ab = getSupportActionBar();
        // set defaults for logo & home up
        ab.setDisplayHomeAsUpEnabled(true);
        ab.setDisplayUseLogoEnabled(true);
        // if (savedInstanceState != null) {
        // Log.d(TAG, "savedInstanceState != null");
        // String savedUrl = savedInstanceState.getString(EXTRA_WEB_URL);
        // if (savedUrl == null) {
        // Log.d(TAG, "savedInstanceState.getString(EXTRA_WEB_URL) == null");
        // loadHomePage();
        // } else {
        // mWebViewClient.shouldOverrideUrlLoading(mWebView, savedUrl);
        // }
        // } else {
        // Log.d(TAG, "savedInstanceState == null");
        // loadHomePage();
        // }
        if (savedInstanceState != null) {
            restoreWebView(savedInstanceState);
        } else {
            loadHomePage();
        }
    }

    private void restoreWebView(Bundle savedInstanceState) {
        mWebView.restoreState(savedInstanceState);
        mWebViewClient.onPause();
        mWebView.clearView();
        String savedUrl = savedInstanceState.getString(EXTRA_WEB_URL);
        mWebViewClient.shouldOverrideUrlLoading(mWebView, savedUrl);
    }

    private void loadHomePage() {
        Log.d(TAG, "loadHomePage");
        mWebView.loadUrl(HOME_URL);
        // new WebPageLoaderTask(mWebView, this, mGoogleAnalyticsEventsHelper).execute(HOME_URL);
    }

    @Override
    protected void onPause() {
        super.onPause();
        mGoogleAnalyticsEventsHelper.dispatch();
        mWebViewClient.onPause();
    }

    @Override
    public void onBackPressed() {
        if (mWebView.canGoBack()) {
            mWebView.goBack();
            WebBackForwardList copyBackForwardList = mWebView.copyBackForwardList();
            WebHistoryItem previous = copyBackForwardList.getItemAtIndex(copyBackForwardList
                    .getCurrentIndex() - 1);
            mWebViewClient.shouldOverrideUrlLoading(mWebView, previous.getUrl());
        } else {
            if (mWebView.getUrl().equals(HOME_URL)) {
                super.onBackPressed();
            } else {
                loadHomePage();
            }
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        getSupportMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_about:
                Toast.makeText(getApplicationContext(), R.string.activity_main_about,
                        Toast.LENGTH_LONG).show();
            case android.R.id.home:
                loadHomePage();
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        mWebView.saveState(outState);
        if (mWebView != null) {
            String url = mWebView.getUrl();
            outState.putString(EXTRA_WEB_URL, url);
        }

        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);

        // mWebView.restoreState(savedInstanceState);
        // mWebView.clearView();
        // if (mWebView != null && savedInstanceState != null) {
        // mWebView.loadUrl(savedInstanceState.getString(EXTRA_WEB_URL));
        // }
        restoreWebView(savedInstanceState);
    }
}
